﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SearchFlight : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session["DepartDates"] = null;
            Session["ReturnDates"] = null;
            Session["requestFromCity"] = null;
            Session["requestToCity"] = null;
        }
    }

    //protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    //{
    //    System.Web.UI.WebControls.Calendar TheCalendar = sender as System.Web.UI.WebControls.Calendar;

    //    // create new list of dates or get stored list of dates
    //    List<DateTime> SelectedDates;

    //    if (ViewState["DepartDates"] == null)
    //        SelectedDates = new List<DateTime>();
    //    else
    //        SelectedDates = ViewState["DepartDates"] as List<DateTime>;

    //    // if date is already in list, remove it, otherwise, add it
    //    if (SelectedDates.Contains(TheCalendar.SelectedDate) == false)
    //        SelectedDates.Add(Calendar1.SelectedDate);
    //    else
    //        SelectedDates.Remove(Calendar1.SelectedDate);

    //    // set the calendar to our list of dates
    //    TheCalendar.SelectedDates.Clear();
    //    foreach (DateTime Date in SelectedDates)
    //        TheCalendar.SelectedDates.Add(Date);

    //    // store list for next postback
    //    ViewState["DepartDates"] = SelectedDates;
    //}
    //protected void Calendar2_SelectionChanged(object sender, EventArgs e)
    //{
    //    System.Web.UI.WebControls.Calendar TheCalendar = sender as System.Web.UI.WebControls.Calendar;

    //    // create new list of dates or get stored list of dates
    //    List<DateTime> SelectedDates;

    //    if (ViewState["ReturnDates"] == null)
    //        SelectedDates = new List<DateTime>();
    //    else
    //        SelectedDates = ViewState["ReturnDates"] as List<DateTime>;

    //    // if date is already in list, remove it, otherwise, add it
    //    if (SelectedDates.Contains(TheCalendar.SelectedDate) == false)
    //        SelectedDates.Add(Calendar2.SelectedDate);
    //    else
    //        SelectedDates.Remove(Calendar2.SelectedDate);

    //    // set the calendar to our list of dates
    //    TheCalendar.SelectedDates.Clear();
    //    foreach (DateTime Date in SelectedDates)
    //        TheCalendar.SelectedDates.Add(Date);

    //    // store list for next postback
    //    ViewState["ReturnDates"] = SelectedDates;
    //}
    //protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    //{
    //    if (e.Day.Date.Date.CompareTo(DateTime.Now.Date) < 0)
    //    {
    //        e.Cell.Enabled = false;
    //        e.Cell.Text = "";
    //        e.Cell.BackColor = System.Drawing.Color.White;
    //    }
    //}
    //protected void Calendar2_DayRender(object sender, DayRenderEventArgs e)
    //{
    //    if (e.Day.Date.Date.CompareTo(DateTime.Now.Date) < 0)
    //    {
    //        e.Cell.Enabled = false;
    //        e.Cell.Text = "";
    //        e.Cell.BackColor = System.Drawing.Color.White;
    //    }
    //}

    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        if (RadioButton2.Checked)
        {
            tbCalReturn.Enabled = true;
            tbCalReturn.Visible = true;
            Label4.Visible = true;
        }
        else
        {
            tbCalReturn.Enabled = false;
            tbCalReturn.Visible = false;
            Label4.Visible = false;
        }
    }
    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        if (RadioButton2.Checked)
        {
            tbCalReturn.Enabled = true;
            tbCalReturn.Visible = true;
            Label4.Visible = true;
        }
        else
        {
            tbCalReturn.Enabled = false;
            tbCalReturn.Visible = false;
            Label4.Visible = false;
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {

        if (tbCalDepart.Text == "")
        {
            CustomValidator1.Text = "Please chose your depart date";
            args.IsValid = false;
            return;
        }
        else if ((tbCalReturn.Text == "") && (RadioButton2.Checked))
        {
            CustomValidator1.Text = "Please chose your return date";
            args.IsValid = false;
            return;
        }
        else
        {
            DateTime MaxdepartTime = new DateTime();
            string[] temp = tbCalDepart.Text.Split(Char.Parse(","));
            try
            {
                foreach (string x in temp)
                {
                    DateTime y = DateTime.Parse(x);
                    if (y.CompareTo(DateTime.Now.Date.AddDays(1)) < 0)
                    {
                        CustomValidator1.Text = "Depart date too small";
                        tbCalDepart.Text = "";
                        args.IsValid = false;
                        return;
                    }
                    else if (y.CompareTo(DateTime.Now.AddMonths(11)) > 0)
                    {
                        CustomValidator1.Text = "Depart date too big";
                        tbCalDepart.Text = "";
                        args.IsValid = false;
                        return;
                    }
                    if (MaxdepartTime.CompareTo(y) < 0)
                    {
                        MaxdepartTime = y;
                    }
                }
            }
            catch (Exception)
            {
                CustomValidator1.Text = "Incorrect Depart Date";
                tbCalDepart.Text = "";
                args.IsValid = false;
                return;
            }
            if (RadioButton2.Checked)
            {
                temp = null;
                temp = tbCalReturn.Text.Split(Char.Parse(","));
                try
                {
                    foreach (string x in temp)
                    {
                        DateTime y = DateTime.Parse(x);
                        if (y.CompareTo(DateTime.Now.Date.AddDays(1)) < 0)
                        {
                            CustomValidator1.Text = "Return date too small";
                            tbCalReturn.Text = "";
                            args.IsValid = false;
                            return;
                        }
                        else if (y.CompareTo(DateTime.Now.AddMonths(11)) > 0)
                        {
                            CustomValidator1.Text = "Return date too big";
                            tbCalReturn.Text = "";
                            args.IsValid = false;
                            return;
                        }
                        if (MaxdepartTime.CompareTo(y) > 0)
                        {
                            CustomValidator1.Text = "Return Date must not greater than Return Date";
                            tbCalReturn.Text = "";
                            args.IsValid = false;
                            return;
                        }
                    }
                }
                catch (Exception)
                {
                    CustomValidator1.Text = "Incorrect Return Date";
                    tbCalReturn.Text = "";
                    args.IsValid = false;
                    return;
                }
            }

            args.IsValid = true;
        }
    }

    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        int adult = Convert.ToInt32(ddlAdult.SelectedValue);
        int child = Convert.ToInt32(ddlChild.SelectedValue);
        int senior = Convert.ToInt32(ddlSenior.SelectedValue);

        if ((adult + child + senior) < 1)
        {
            CustomValidator2.Text = "Please Select Number of Passenger";
            args.IsValid = false;
        }
        else if ((adult + child + senior) > 6)
        {
            CustomValidator2.Text = "Maximum 6 Passenger";
            args.IsValid = false;
        }
        else if ((child > 0) && (adult == 0) && (senior == 0))
        {
            CustomValidator2.Text = "Children below the age of 12 (0-11 years) will not be eligible to travel unless they are accompanied by an appropriate Accompanying Passenger*";
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            FlightBAL _flightBAL = new FlightBAL();

            string _origin = tbFromCity.Text;
            string _originCountry = tbFromCountry.Text;
            string _destination = tbToCity.Text;
            string _destinationCountry = tbToCountry.Text;
            int adult = Convert.ToInt32(ddlAdult.SelectedValue);
            int child = Convert.ToInt32(ddlChild.SelectedValue);
            int senior = Convert.ToInt32(ddlSenior.SelectedValue);

            //--Step 1.Search Location
            List<Location> _originList = new List<Location>();
            List<Location> _destinationList = new List<Location>();
            try
            {
                _originList = _flightBAL.SearchPlacefromWebServices(_origin, _originCountry);
               _destinationList = _flightBAL.SearchPlacefromWebServices(_destination, _destinationCountry);
            }
            catch (Exception)
            {
                Label5.Text = "Service unavailable at the momment. Please try again latter.";
                Label5.Visible = true;
                return;
            }
            if (_originList.Count == 0)
            {
                Label5.Text = "From City not found. Please check again.";
                Label5.Visible = true;
            }
            else if (_destinationList.Count == 0)
            {
                Label5.Text = "To City not found. Please check again.";
                Label5.Visible = true;
            }
            else
            {
                Session["requestFromCity"] = _originList;
                Session["requestToCity"] = _destinationList;

                Session["Adult"] = ddlAdult.SelectedIndex;
                Session["Child"] = ddlChild.SelectedIndex;
                Session["Senior"] = ddlSenior.SelectedIndex;

                List<DateTime> DepartDate = new List<DateTime>();
                string[] departstring = tbCalDepart.Text.Split(char.Parse(","));

                foreach (string x in departstring)
                {
                    DepartDate.Add(DateTime.Parse(x));

                }
                Session["DepartDates"] = DepartDate;

                if (RadioButton2.Checked)
                {
                    List<DateTime> ReturnDates = new List<DateTime>();
                    string[] returnstring = tbCalReturn.Text.Split(char.Parse(","));

                    foreach (string x in returnstring)
                    {
                        ReturnDates.Add(DateTime.Parse(x));

                    }
                    Session["ReturnDates"] = ReturnDates;
                }
                else
                {
                    Session["ReturnDates"] = null;
                }
                Response.Redirect("~/SearchFlight1.aspx");
            }
        }
        //List<DateTime> _returnDateList = new List<DateTime>();
        //if (RadioButton2.Checked)
        //{
        //    foreach (DateTime date in Calendar2.SelectedDates)
        //    {
        //        _returnDateList.Add(date);
        //    }
        //}

    }
}
